home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’97 / Warrior’s Progress / source code / Source / Libraries / Arrays / ArrayLoop.h < prev    next >
Encoding:
Text File  |  1997-06-28  |  1.6 KB  |  66 lines  |  [TEXT/CWIE]

  1. // ArrayLoop.h
  2.  
  3. #ifndef ArrayLoop_h
  4. #define ArrayLoop_h
  5.  
  6. #ifndef ArrayOf_h
  7. #include "ArrayOf.h"
  8. #endif
  9.  
  10. template < class Element >
  11. class ArrayLoop
  12.   {
  13.     private:
  14.         Element *position;
  15.         Element *const stop;
  16.         Element *const elementZero;
  17.     
  18.     public:
  19.         inline ArrayLoop( ArrayOf<Element> array );
  20.         inline ArrayLoop( ArrayOf<Element> array, uint32 begin, uint32 end );
  21.         inline ArrayLoop( ArrayOf<Element> array, Range<uint32> range );
  22.         
  23.         bool Finished() const            { return position >= stop; }
  24.         bool Unfinished() const            { return position < stop; }
  25.         
  26.         Element *operator++()            { return ++position; }
  27.         Element *operator++(int)        { return position++; }
  28.         
  29.         Element& operator*() const        { return *position; }
  30.         Element *operator->() const    { return position; }
  31.   };
  32.  
  33. template < class Element >
  34. ArrayLoop<Element>::ArrayLoop( ArrayOf<Element> array )
  35.   : position( array.Start() ),
  36.      elementZero( array.Start() ),
  37.      stop( array.End() )
  38.   {
  39.     Assert( !array.Null() );
  40.   }
  41.  
  42. template < class Element >
  43. ArrayLoop<Element>::ArrayLoop( ArrayOf<Element> array,
  44.                                          uint32 begin,
  45.                                          uint32 end )
  46.   : position( array.Start() + begin ),
  47.      elementZero( array.Start() ),
  48.      stop( array.Start() + end )
  49.   {
  50.     Assert( begin <= end );
  51.     Assert( end <= array.Length() );
  52.   }
  53.  
  54. template < class Element >
  55. ArrayLoop<Element>::ArrayLoop( ArrayOf<Element> array,
  56.                                          Range<uint32> range )
  57.   : position( array.Start() + range.Start() ),
  58.      elementZero( array.Start() ),
  59.      stop( array.Start() + range.End() )
  60.   {
  61.     Assert( range.Start() <= range.End() );
  62.     Assert( range.End() <= array.Length() );
  63.   }
  64.  
  65. #endif
  66.